//
//  Dictionary.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/6/7.
//

import Foundation

public extension Dictionary{
    
    var json: String {
        let invalidJson = "Not a valid JSON"
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: self)
            return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson
        } catch {
            return invalidJson
        }
    }
    
    
    func add(_ dict: Dictionary, useFirstDefault isFirst: Bool = false) -> Dictionary {
        return merging(dict) { isFirst ? $0 : $1 }
    }
    
}

